﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// Summary description for FuntionHNC
/// </summary>
public class FuntionHNC
{

    public static string GetLanguage(string strVN, string strEN)
    {
        var currentLanguage = "vi-VN";
        if (HttpContext.Current.Session != null)
        {
            currentLanguage = HttpContext.Current.Session["language"] == null ? "vi-VN" : HttpContext.Current.Session["language"].ToString();
        }

        switch (currentLanguage)
        {
            case "vi-VN":
                return strVN;
            case "en-US":
                return strEN;
            default:
                return strVN;
        }
    }

    #region Bo dau tiengviet
    public static string RemoveUnicode(string text)
    {
        string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",  
    "đ",  
    "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
    "í","ì","ỉ","ĩ","ị",  
    "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
    "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
    "ý","ỳ","ỷ","ỹ","ỵ",};
        string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",  
    "d",  
    "e","e","e","e","e","e","e","e","e","e","e",  
    "i","i","i","i","i",  
    "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
    "u","u","u","u","u","u","u","u","u","u","u",  
    "y","y","y","y","y",};
        for (int i = 0; i < arr1.Length; i++)
        {
            text = text.Replace(arr1[i], arr2[i]);
            text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());
        }
        return text;
    }
    #endregion

    #region Cat chuoi n khoang trang
    public static string Catchuoi(string _chuoi, int _spaces)
    {
        string[] temp = _chuoi.Split(' ');
        if (temp.Length < _spaces)
            return _chuoi;
        else
        {
            string temp2 = string.Empty;
            for (int i = 0; i < _spaces; i++)
            {
                temp2 = temp2 + ' ' + temp[i];
            }
            temp2 += "....";
            return temp2;
        }
    }
    #endregion

    //#region Cat n ki thu dau

    ///// <summary>
    ///// Trả về phần đầu của chuỗi và đảm bảo đủ từ
    ///// </summary>
    ///// <param name="s"></param>
    ///// <param name="length">Số ký tự tối đa được cắt</param>
    ///// <returns>String</returns>
    //public static string catchuoi2(this string s, int length)
    //{
    //    if (String.IsNullOrEmpty(s))
    //        throw new ArgumentNullException(s);
    //    var words = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    //    if (words[0].Length > length)
    //        throw new ArgumentException("Từ đầu tiên dài hơn chuỗi cần cắt");
    //    var sb = new StringBuilder();
    //    foreach (var word in words)
    //    {
    //        if ((sb + word).Length > length)
    //            return string.Format("{0}...", sb.ToString().TrimEnd(' '));
    //        sb.Append(word + " ");
    //    }
    //    return string.Format("{0}...", sb.ToString().TrimEnd(' '));
    //}

    //#endregion

    #region ReplaceSpace
    public static string ReplaceSpace(string text)
    {
        string reg = " +";
        text = Regex.Replace(text, reg, "-");
        return text;
    }
    #endregion

    #region lay cac ky tu sau ky tu nao do
    // lay cac ky tu sau ky tu @
    public static string FormatRegion(string val)
    {
        string retval = string.Empty;
        string[] arr = val.Split(':');
        for (int i = 1; i < arr.Length; i++)
        {
            int num;
            if (!int.TryParse(arr[i].ToString(), out num))
            {
                retval += arr[i].ToString();
            }
            else
            {
                retval += " ; ";
            }
        }
        return retval;
    }
    #endregion

    #region Encryptdata and Decryptdata
    private string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }

    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;
    }
    #endregion

    #region kiem tra dinh dang va upload anh
    public static bool CheckFileType(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".png":

                return true;

            case ".jpg":

                return true;

            default:
                return false;
        }

    }
    #endregion

    #region Xuong dong
    public static string xuongdong(string strSource)
    {
        string a = "";
        if (!String.IsNullOrEmpty(strSource))
        {
            a = strSource.Replace("\r\n", "<br/>");
        }
        return a;
    }
    #endregion  

    public static void sendmail(string from, string to, string cc, string subject, string body)
    {
        try
        {
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress("serverhungnguyen@gmail.com", "Liên hệ từ Website"); //mailMsg.From = new MailAddress("Email_cua_ban@gmail.com", "ten_ban");
            mailMsg.To.Add(to);

            mailMsg.CC.Add(cc);  // cái này ko cần thiết lắm
            // mailMsg.Bcc.Add(bcc);// cái này ko cần thiết lắm

            mailMsg.Subject = subject;
            mailMsg.IsBodyHtml = true;
            mailMsg.Body = body;
            mailMsg.Priority = MailPriority.Normal;
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            NetworkCredential info = new NetworkCredential("serverhungnguyen@gmail.com", "123123@#");
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Credentials = info;
            client.EnableSsl = true;
            client.Send(mailMsg);

            // Response.Write("Đã gởi thư thành công");

        }
        catch (Exception)
        {
            // Response.Write("Không gởi được!");
        }

    }

    public static string RemoveHtmlTagsUsingCharArray(string htmlString)
    {
        var array = new char[htmlString.Length];
        var arrayIndex = 0;
        var inside = false;

        foreach (var @let in htmlString)
        {
            if (let == '<')
            {
                inside = true;
                continue;
            }
            if (let == '>')
            {
                inside = false;
                continue;
            }
            if (inside) continue;
            array[arrayIndex] = let;
            arrayIndex++;
        }
        return new string(array, 0, arrayIndex);
    }

    public static string ThemChuoiDatetime()
    {
        string n = DateTime.Now.ToString("yyyy/MM/dd/ HH:mm:ss").Replace("/", "").Replace(" ", "").Replace(":", "");
        return n;
    }
}